﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using DunSilverlight.Comm;
using DunLibrary.Client;

namespace DunSilverlight.Views
{
    /// <summary>
    /// 银行信息列表
    /// owen
    /// 2011-12-12
    /// </summary>
    public partial class BankInfoPage : Page
    {
        public BankInfoPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(BankInfoPage_Loaded);
        }

        /// <summary>
        /// 窗体显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BankInfoPage_Loaded(object sender, RoutedEventArgs e)
        {
            Query();//加载所有银行数据
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            Query();
        }

        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
        }

        private void RadTreeViewItem_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {

        }

        /// <summary>
        /// 执行查询
        /// 基础资料都比较少，不需要分页，也没有查询条件
        /// </summary>
        private void Query()
        {
            //在web定义一个wcf服务，这个服务继承BaseService,然后在前台添加一个服务(如果还没有添加的话)
            BankService.BankServiceClient ser = new BankService.BankServiceClient();

            //wcf所有函数都是异步执行，所以定义并添加一个回调函数
            ser.GetListByHQLCompleted += (object sender, BankService.GetListByHQLCompletedEventArgs e) =>
                {
                    try
                    {
                        int totalCount = 0;
                        //反序列化一个Company的集合
                        IList<DunLibrary.Client.Company> banks = JsonSerializerHelper.JsonToEntities<DunLibrary.Client.Company>(e.Result, out totalCount);

                        dgdBank.ItemsSource = banks;
                    }
                    catch (Exception ex)
                    {
                        Tools.ShowMessage(ex.Message, "", false);
                    }
                };
            //开始异步调用后台wcf服务,传入hql()
            ser.GetListByHQLAsync("from Company");
        }
        /// <summary>
        /// 新增一个银行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Company cmp=new Company();

            BankInfoForm frmBank = new BankInfoForm(cmp);

            Tools.OpenWindow("银行信息-新增", frmBank, null);
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            Company cmp = dgdBank.SelectedItem as Company;

            if (cmp != null)
            {
                BankInfoForm frmBank = new BankInfoForm(cmp);
                Tools.OpenWindow("银行信息-编辑", frmBank, null);
            }
        }
        /// <summary>
        /// 删除一个银行信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            Company cmp=dgdBank.SelectedItem as Company;

            if(cmp!=null)
            {

                MessageBoxResult mr=MessageBox.Show(string.Format("是否要删除{0}",cmp.Name),"提示",MessageBoxButton.OKCancel);

                if (mr == MessageBoxResult.OK)
                {
                    BankService.BankServiceClient ser = new BankService.BankServiceClient();

                    ser.DeleteByIdCompleted += (object sender1, BankService.DeleteByIdCompletedEventArgs e1) =>
                        {
                            if (JsonSerializerHelper.JsonToEntity<bool>(e1.Result))
                            {
                                Tools.ShowMessage("删除成功!","",true);
                            }
                        };

                    ser.DeleteByIdAsync(cmp.Id);
                }
            }
        }


    }
}
